
import {reqgetgoodsinfo,reqCartAdd} from "../../request/api";

//初始值
const initState={
    //定义接收数据的数组
    list:[],
};

//types
const types={
    CHANGE_LIST:"DETAIL_CHANGE_LIST",
};

//修改数据
const reducer = (state=initState,action)=>{
    switch (action.type) {
        case types.CHANGE_LIST:
            return {
                ...state,
                list:action.arr
            }
        default:
            return state;
    }
};

//组件派发的任务
export const actions = {
    //修改list
    changeList:(arr) =>({type:types.CHANGE_LIST,arr}),
    //进行请求
    reqGoodsInfo:(id)=>(dispatch,getState)=>{
        reqgetgoodsinfo({
            id:id
        }).then(res=>{
            if(res.data.code===200){
                let ok = res.data.list?res.data.list:[]
                dispatch(actions.changeList(ok));
            }
        })
    },
    //点击了添加购物车
    cateAdd:(id,uid,history)=>(dispatch,getState)=>{
        reqCartAdd({
            uid,
            type:1,
            goodsid:id,
            num:1
        }).then(res=>{
            if(res.data.code===200){
                alert(res.data.msg)
            }
        })
    }
};

//导出数据!
//导出list
export const getList = (state)=>state.detail.list;

//整体导出
export default reducer;
